import  { createContext, useState, useEffect } from 'react';
import { useLocation } from 'react-router';
export const AuthContext = createContext();

// eslint-disable-next-line react/prop-types
export function AuthProvider({ children }) {
  const [isAuthenticated, setIsAuthenticated] = useState(true);//必须是true，不然一刷新就会被拦截
  const location = useLocation(); // 使用useLocation钩子

  useEffect(() => {
    const token = localStorage.getItem('userInfo');
    setIsAuthenticated(!!token);
  }, [location]);

  return (
    <AuthContext.Provider value={{ isAuthenticated, setIsAuthenticated }}>
      {children}
    </AuthContext.Provider>
  );
}